package com.example.jksj.bookmanagement.pro.mine.activity;

import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.jksj.bookmanagement.R;
import com.example.jksj.bookmanagement.event.StudengInfoEvent;
import com.example.jksj.bookmanagement.common.MyActivity;
import com.example.jksj.bookmanagement.utils.ShareUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.greenrobot.eventbus.EventBus;

/*
 *  项目名：    BookManagement
 *  包名：      com.example.jksj.bookmanagement.ui.activity.MineFragmentOf.Setting
 *  文件名:     AuthenticationActivity
 *  创建者:     HXD
 *  创建时间:   2019/2/10 9:41
 *  描述:       实名认证界面
 */
public class AuthenticationActivity extends MyActivity {

    private EditText etSchool;      //学校
    private EditText etStudentID;   //学号
    private Button btnTrim;         //确定

    private QMUITipDialog tipDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_authentication;
    }

    @Override
    protected int getTitleBarId() {
        return R.id.tb_authentication_title;
    }

    @Override
    protected void initView() {
        etSchool = findViewById(R.id.et_school_Authentication);
        etStudentID = findViewById(R.id.et_studentID_Authentication);
        btnTrim = findViewById(R.id.btn_trim_Authentication);

        btnTrim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进行验证操作
                isSuccess();
            }
        });
    }

    @Override
    protected void initData() {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("验证中...")
                .create();
    }

    //判断是否验证通过
    private void isSuccess() {
        String schoolID = etSchool.getText().toString().trim();
        String studentID = etStudentID.getText().toString().trim();
        //在这里进行联网访问数据库操作

        if (schoolID.equals("湖南科技学院") && studentID.equals("201706030220")) {
            tipDialog.show();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    tipDialog.dismiss();
                    //访问成功的回调
                    toast("实名认证成功");
                    //把正确的信息发送到账号管理中
                    EventBus.getDefault().post(new StudengInfoEvent("湖南科技学院",
                            "201706030220", "何相达", "男"));

                    //把信息保存到缓存中去
                    ShareUtils.putString("StudengInfo_shcool", "湖南科技学院");
                    ShareUtils.putString("StudengInfo_studentID", "201706030220");
                    ShareUtils.putString("StudengInfo_name", "何相达");
                    ShareUtils.putString("StudengInfo_gender", "男");
                    //用于检测是否实名认证
                    ShareUtils.putBoolean("StudentInfo", true);

                    finish();
                }
            }, 2000);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
